# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: wirx6 <wirx654@gmail.com>

_realname=Vulkan-Loader
pkgbase=mingw-w64-vulkan
pkgname=("${MINGW_PACKAGE_PREFIX}-vulkan")
conflicts=("${MINGW_PACKAGE_PREFIX}-vulkan-headers")
replaces=("${MINGW_PACKAGE_PREFIX}-vulkan-headers")
pkgver=1.1.73.0
pkgrel=1
pkgdesc='Khronos official ICD loader and validation layers for Vulkan (mingw-w64)'
arch=('any')
url="https://www.khronos.org/vulkan/"
groups=("${MINGW_PACKAGE_PREFIX}-vulkan-devel")
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-glslang"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-spirv-tools"
             "git")
_spirv_tools_commit=8d8a71278bf9e83dd0fb30d5474386d30870b74d
source=(${_realname}-sdk-${pkgver}.tar.gz::"https://github.com/KhronosGroup/${_realname}/archive/sdk-${pkgver}.tar.gz"
        001-build-fix.patch
        002-proper-def-files-for-32bit.patch
        003-generate-pkgconfig-files.patch
        004-installation-commands.patch
        005-mingw-dll-name.patch
        006-skip-commit-create.patch)
sha256sums=('1692c84e1a753111fd994265ca5030f8983070b242a75a023fb0f4e58dbf6d53'
            'deda6fd3d5a67ff2e8d885084ca56189ad1c7d9dcfbd4b57211a45479f94273d'
            'b40e3306343f85c6f94d9593dce22730c16e16ae1f4734ca27495468a070765d'
            '5c189b3f76fa53ad12077cd8932423a0cf385f9464cdde60ba711589ccde19ad'
            '1559474b79aaf0d7fc2f03c7aa3c49a48d5aae3e9db89f4a328414ee133de9d7'
            '605286f2b93a9f182a39fcc1d66e7bedf98c913c3e8ad177c687092de11dc4d4'
            'd8190d309e104dbe527c846310dbe0f02dbea73d26d104accfa468307a3e2c45')

prepare() {
  cd ${srcdir}/${_realname}-sdk-${pkgver}
  patch -p1 -i ${srcdir}/001-build-fix.patch
  patch -p1 -i ${srcdir}/002-proper-def-files-for-32bit.patch
  patch -p1 -i ${srcdir}/003-generate-pkgconfig-files.patch
  patch -p1 -i ${srcdir}/004-installation-commands.patch
  patch -p1 -i ${srcdir}/005-mingw-dll-name.patch
  patch -p1 -i ${srcdir}/006-skip-commit-create.patch

  echo "#define SPIRV_TOOLS_COMMIT_ID \"${_spirv_tools_commit}\"" > spirv_tools_commit_id.h
  cp -f spirv_tools_commit_id.h loader/
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  CFLAGS+=" -D_WIN32_WINNT=0x0600 -D__STDC_FORMAT_MACROS" \
  CPPFLAGS+=" -D_WIN32_WINNT=0x0600 -D__STDC_FORMAT_MACROS" \
  CXXFLAGS+=" -D_WIN32_WINNT=0x0600 -D__USE_MINGW_ANSI_STDIO -D__STDC_FORMAT_MACROS -fpermissive" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_DEMOS=OFF \
    -DBUILD_TESTS=OFF \
    -DDISABLE_BUILD_PATH_DECORATION=ON \
    -DDISABLE_BUILDTGT_DIR_DECORATION=ON \
    ../${_realname}-sdk-${pkgver}

  make
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/${_realname}-sdk-${pkgver}/LICENSE.TXT ${pkgdir}${MINGW_PREFIX}/share/licenses/vulkan/LICENSE
}
